<script>
    to_facturate = new Array();
    client_to_facturate = ""
</script>
<div id="div_liste_bon_livraison" class="row-fluid">
    <div class="span12">
        <div class="box gradient">
            <div class="title" style="text-align: center">
                <h4>Liste Bons Livraison</h4>
            </div>
            <div id="div-list-user" class="content noPad clearfix">
                <div id="DataTables_Table_0_wrapper" class="dataTables_wrapper" role="grid">
                    <div id="DataTables_Table_0_filter" class="dataTables_filter">
                        <table>
                            <tr style="width: 100%">
                                <td style="width: 15%">
                                    <label>
                                        Departement:
                                        {{ render(controller('GdsVenteBundle:Devis:getAllDepartement')) }}
                                    </label>
                                </td>
                                <td style="width: 15%">
                                    <label>
                                        Type:
                                        <select id="filtre_type" class="nostyle"  style="width:80%;margin-left: 0" required="required">
                                            <option value="2">Tous</option>
                                            <option value="1">Facturé</option>
                                            <option value="0">Non Facturé</option>
                                        </select>
                                    </label>
                                </td>
                                <td style="width: 15%">
                                    <label>
                                        Reglement:
                                        <select id="filtre_reglement" class="nostyle"  style="width:80%;margin-left: 0" required="required">
                                            <option value="0">Tous</option>
                                            <option value="1">Non Réglé</option>
                                            <option value="2">Réglé</option>
                                            <option value="3">Partiellement Réglé</option>
                                        </select>
                                    </label>
                                </td>
                                <td style="width: 15%">
                                    <label>
                                        Raison Social:
                                        {{ render(controller('GdsVenteBundle:Client:getAllClient')) }}
                                        <script> new autocomplete(document.getElementById("client"))</script>
                                    </label>
                                </td>
                                <td>
                                    <label>
                                        Reste:
                                        <a id="reste_montant"></a>
                                    </label>
                                </td>
                            </tr>
                            <tr style="width: 100%">
                                <td style="width: 10%">
                                    <label>
                                        N° BL:
                                        <input class="text" type="text" id="filtre_numero" style="width:80%;margin-left: 0"/>
                                    </label>
                                </td>

                                <td style="width: 10%">
                                    <label>
                                        Date:
                                        <input class="text" type="text" id="filtre_date1" style="width:80%;margin-left: 0"/>
                                    </label>
                                </td>
                                <td style="width: 10%">
                                    <label>
                                        Jusqu'à:
                                        <input class="text" type="text" id="filtre_date2" style="width:80%;margin-left: 0" />
                                    </label>
                                </td>
                                <td style="width: 10%">
                                    <label>
                                        Heure:
                                        <select id="filtre_heure" class="nostyle"  style="width:80%;margin-left: 0"required="required">
                                            <option value="1"></option>
                                            <option value="2">07h -> 12h</option>
                                            <option value="3">12h -> 21h</option>
                                            <option value="4">08h -> 10h</option>
                                            <option value="5">10h -> 12h</option>
                                            <option value="6">12h -> 14h</option>
                                            <option value="7">14h -> 16h</option>
                                            <option value="8">16h -> 18h</option>
                                            <option value="9">18h -> 20h</option>
                                            <option value="10">20h -> 22h</option>
                                        </select>
                                    </label>
                                </td>
                                <td style="width: 10%">
                                    <label>
                                        Reference:
                                        <input class="text" type="text" id="filtre_reference" style="width:80%;margin-left: 0"/>
                                    </label>
                                </td>
                                <td style="width: 10%">
                                    <label>
                                        Designation:
                                        <input class="text" type="text" id="filtre_designation" style="width:80%;margin-left: 0"/>
                                    </label>
                                </td>
                                <td style="width: 10%">
                                    <label>
                                        TTC entre:
                                        <input class="text" type="text" id="filtre_ttc1" style="width:80%;margin-left: 0"/>
                                    </label>
                                </td>
                                <td style="width: 10%">
                                    <label>
                                        ET:
                                        <input class="text" type="text" id="filtre_ttc2" style="width:80%;margin-left: 0"/>
                                    </label>
                                </td>
                            </tr>
                        </table>


                    </div>


                    <div id="liste_bon_livraison">
                {% include 'GdsVenteBundle:BonLivraison:page_liste_bon_livraison.html.twig' %}
                    </div>
                </div>
            </div>
        </div>
    </div><!-- End .box -->

</div><!-- End .span12 -->

<script>
    $(document).ready(function() {

        heure1 = ''
        heure2 = ''
        date1 = ''
        date2 = ''
        var fullDate = new Date();
        var twoDigitMonth = fullDate.getMonth()
        twoDigitMonth++
        twoDigitMonth += "";
        if (twoDigitMonth.length == 1)
            twoDigitMonth = "0" + twoDigitMonth;
        var twoDigitDate = fullDate.getDate() + "";
        if (twoDigitDate.length == 1)
            twoDigitDate = "0" + twoDigitDate;
        var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear();
        $("#filtre_date2").val(currentDate)
        $("#filtre_date1").val('')
        $('#filtre_heure').val(1)
        $('#filtre_numero').val('')
        $('#filtre_reference').val('')
        $('#filtre_designation').val('')
        $('#filtre_ttc1').val('')
        $('#filtre_ttc2').val('')
        $('#filtre_type').val(2)
        $('#client').val(0)
        $('#filtre_reglement').val(0)
        $('#departement').val({{departement}})
        date2 = changeFormatDate($('#filtre_date2').val())
    })
    $("#filtre_date1").datepicker({
        changeMonth: true,
        onClose: function(selectedDate) {
            $("#filtre_date2").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#filtre_date2").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        onClose: function(selectedDate) {
            $("#filtre_date1").datepicker("option", "maxDate", selectedDate);
        }
    });
    $('#filtre_heure').change(function() {
        heure = $("#filtre_heure option:selected").text();
        heure = heure.split('h')
        heure1 = heure[0] * 3600
        if (heure1 != 0) {
            heure2 = heure[1]
            heure2 = heure2.split('->')
            heure2 = heure2[1] * 3600
        } else {
            heure1 = ''
            heure2 = ''
        }
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#filtre_numero').keyup(function() {
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#client').change(function() {
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#filtre_date1').change(function() {
        date1 = changeFormatDate($('#filtre_date1').val())
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#filtre_date2').change(function() {
        date2 = changeFormatDate($('#filtre_date2').val())
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#filtre_reference').keyup(function() {
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#filtre_designation').keyup(function() {
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#filtre_ttc1').keyup(function() {
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#filtre_ttc2').keyup(function() {
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#filtre_designation').autocomplete({
        source: "{{ path('gds_vente_get_designations_autocomplete') }}",
        select: function(event, ui) {
            execute_search = false
            if (ui.item) {
                $('#filtre_reference').val(ui.item.reference);
            }
        }
    });
    $('#filtre_reference').autocomplete({
        source: "{{ path('gds_vente_get_references_autocomplete') }}",
        select: function(event, ui) {
            if (ui.item) {
                $('#filtre_reference').val(ui.item.designation);
            }
        }
    });
    $('#filtre_designation').change(function() {
        $.ajax({
            url: "{{ path('gds_vente_get_reference_where_designation') }}",
            data: 'designation=' + $('#filtre_designation').val(),
            success: function(data) {
                $('#filtre_reference').val(data)
            }
        });
    });
    $('#filtre_reference').change(function() {
        $.ajax({
            url: "{{ path('gds_vente_get_designation_where_reference') }}",
            data: 'reference=' + $('#filtre_reference').val(),
            success: function(data) {
                $('#filtre_designation').val(data)
            }
        });
    });
    $('#filtre_type').change(function() {
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#departement').change(function() {
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    $('#filtre_reglement').change(function() {
        goToPage('{{path(pagination.route)}}?type=ajax')
    })
    function goToPage(url) {
        $.ajax({
            url: url,
            mode:'abort',
            data: '&type=ajax&numero=' + $('#filtre_numero').val() + '&client=' + $('#client').val()
                    + '&date1=' + date1 + '&date2=' + date2
                    + '&heure1=' + heure1 + '&heure2=' + heure2
                    + '&reference=' + $('#filtre_reference').val() + '&designation=' + $('#filtre_designation').val()
                    + '&ttc1=' + $('#filtre_ttc1').val() + '&ttc2=' + $('#filtre_ttc2').val() + '&is_facturer=' + $('#filtre_type').val()
                    + '&departement=' + $('#departement').val() + '&reglement=' + $('#filtre_reglement').val(),
            success: function(data) {
                $('#liste_bon_livraison').html(data)
            }
        })
    }
    function changeFormatDate(date) {
        if (date == '')
            return date
        date = date.split('/')
        date = date[2] + '-' + date[1] + '-' + date[0]
        return date
    }
    function facturer(id, client) {
        if (is_facturate(id)) {
            to_facturate.splice($.inArray(id, to_facturate), 1);
            $('#facturer_' + id).html('')
            if (to_facturate.length == 0)
                $('#facturer_plusieurs').hide()
        } else {
            if (to_facturate.length == 0) {
                $('#facturer_plusieurs').show()
                client_to_facturate = client
                $('#facturer_' + id).html("<img id='theImg'  src='/images/clicked.jpg'/>");
                to_facturate[to_facturate.length] = id
            }
            else {
                if (client_to_facturate != client) {
                    $.msgBox({
                        title: "Client différent",
                        content: "Il faut séléctionner des BL de même client",
                        type: "error",
                        buttons: [{value: "OK"}]
                    });
                } else {
                    $('#facturer_' + id).html("<img id='theImg'  src='/images/clicked.jpg'/>");
                    to_facturate[to_facturate.length] = id
                }
            }
        }
    }
    function is_facturate(id) {
        var count_to_facturate = to_facturate.length
        for (var i = 0; i < count_to_facturate; i++) {
            if (to_facturate[i] == id)
                return true
        }
        return false
    }
    function showFacturer() {
        for (i = 0; i < to_facturate.length; i++) {
            $('#facturer_' + to_facturate[i]).html("<img id='theImg'  src='/images/clicked.jpg'/>");
        }
        if (to_facturate.length > 0)
            $('#facturer_plusieurs').show()
    }

</script>

